﻿using System;
using System.Collections.Generic;

namespace UI
{
    using static Utilitys;
    /// <summary>
    /// UIForm对象的具体显示逻辑，同时提供多个接口对处理过程中插入代码
    /// </summary>
    internal static class UIFormHandlers
    {
        private static List<IUIFormHandler> _hideHandlers = new List<IUIFormHandler>();
        private static List<IUIFormHandler> _closeHandler = new List<IUIFormHandler>();
        private static List<IUIFormHandler> _freezHandler = new List<IUIFormHandler>();
        private static List<IUIFormHandler> _showHandler = new List<IUIFormHandler>();
        internal static void AddHandler(IUIFormHideHandler handler)
        {
            _hideHandlers.Add(handler);
        }
        internal static void AddHandler(IUIFormShowHandler handler)
        {
            _showHandler.Add(handler);
        }
        internal static void AddHandler(IUIFormFreezHandler handler)
        {
            _freezHandler.Add(handler);
        }
        internal static void AddHandler(IUIFormCloseHandler handler)
        {
            _closeHandler.Add(handler);
        }
        internal static void Open(UIForm form)
        {
            if (form.Info.AutoToTop)
            {
                form.ToTop();
            }
            form.Score += 1000;
            DoHandler(form, _showHandler, form.Instance.Open);
        }

        internal static void Freez(UIForm form)
        {
            DoHandler(form, _freezHandler, form.Instance.Freez);
        }

        internal static void Close(UIForm form)
        {
            DoHandler(form, _closeHandler, form.Instance.Close);
        }

        private static void DoHandler(UIForm form, List<IUIFormHandler> handlers,Action action)
        {
            ForAction(handlers, (h) => h.BforeHandler(form));
            action();
            ForAction(handlers, (h) => h.AfterHandler(form));
        }
    }


}
